<?php
require_once 'pages/TMSAdminPage.inc';

class TShowCaseSetting extends TProperty {
	private $_value_;
	
/**
 * gets property value
 * @return mixed
 */		
	public function Value(){
		return $this->_value_;
	}
/**
 * gets property item
 * @return IItem
 */		
	public function Item(){return null;}	
	
	public function __construct(IPropertyMeta $meta, $value){
		parent::__construct($meta);
		$this->_value_ = $value;
	}	
}

class TMSSettingsPage extends TMSAdminPage {
	public $SaveActionTitle = 'Save';
	public $CancelActionTitle = 'Cancel';
	
	protected $_array_bool_settings_ = array();
	private $_selection_lists_ = array();
	
	private $_captions_ = array();
	
	
	protected function getSettings(){
		$result = array();
		$values = $this->Service()->ShopSettings->ListValues();
	
		foreach ($values as $nm => $v){
			$type = TItemPropertyType::PT_STRING;
			$selection = array();
			if (isset($this->_array_bool_settings_[$nm]))
				$type = TItemPropertyType::PT_BOOL;
			else if (isset($this->_selection_lists_[$nm]))
				$selection = $this->_selection_lists_[$nm];
			
			$caption = $nm;
			if (isset($this->_captions_[$nm]))
				$caption = $this->_captions_[$nm];
			
			$result[] = new TShowCaseSetting(new TPropertyMeta($nm, $caption, $type,$selection), $v);
		}
		
		return $result;
	}
	
	public function __set($nm,$value){
		switch ($nm){
			case 'SelectionLists':$this->_selection_lists_ = TConvertions::ConvertToArray($value,array(';',':',',','='),array(false,true,false,true));break;
			case 'Captions':$this->_captions_ = TConvertions::ConvertToArray($value,array(';','='),array(false,true));break;
			default:parent::__set($nm,$value);break;
		}
	}
	
	public function __get($nm){
		switch ($nm){
			case 'Properties':return $this->getSettings();break;
			case 'Path':return array();break;
			case 'ListUrl':return false;break;
			case 'SelectionLists':return $this->_selection_lists_;break;
			case 'Captions':return $this->_captions_;break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function http_Save(){
		$settings = $this->getSettings();
		$values = array();
		foreach ($settings as $s){
			$nm = $s->Name();
			if ((string)$s->Type() == TItemPropertyType::PT_BOOL)
				$values[$s->Name()] = TConvertions::ConvertToBoolean($this->request->$nm);
			else	
				$values[$s->Name()] = $this->request->$nm;
		}
		$this->Service()->ShopSettings->SetValues($values);
	}
	
	public function CollectionUrl(IProperty $property){return null;}
}